<?php
/* @var $this EstagioController */
/* @var $model EstagioUsuario */
/* @var $form CActiveForm */
?>

<div class="form">

    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'estagio-usuario-form',
        'enableAjaxValidation' => false,
    ));
    ?>

    <p class="note">Campos com <span class="required">*</span> são obrigadotrios.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php if ($model->isNewRecord): ?>
            <?php echo $form->labelEx($model, 'aluno_id'); ?>
            <?php echo CHtml::textField('', '', array('id' => 'usuario-nome')); ?>
            <?php echo $form->hiddenField($model, 'aluno_id', array('id' => 'usuario-id')); ?>
            <?php echo $form->error($model, 'aluno_id'); ?>
        <?php else: ?>
            <?php echo $form->labelEx($model, 'aluno_id'); ?>
            <?php echo CHtml::encode($model->aluno->nome); ?>
        <?php endif; ?>
    </div>

    <div class="row">
        <?php if ($model->isNewRecord): ?>
            <?php echo $form->labelEx($model, 'empresa_id'); ?>
            <?php echo CHtml::textField('', '', array('id' => 'empresa-nome')); ?>
            <?php echo $form->hiddenField($model, 'empresa_id', array('id' => 'empresa-id')); ?>
            <?php echo $form->error($model, 'empresa_id'); ?>
        <?php else: ?>
            <?php echo $form->labelEx($model, 'empresa_id'); ?>
            <?php echo CHtml::encode($model->empresa->nome); ?>
        <?php endif; ?>
    </div>

    <div class="row">
        <?php if ($model->isNewRecord): ?>
            <?php echo $form->labelEx($model, 'data_inicio'); ?>
            <?php echo $form->dateField($model, 'data_inicio', array('value' => (($model->isNewRecord || !$model->data_inicio) ? date('Y-m-d') : $model->data_inicio))); ?>
            <?php echo $form->error($model, 'data_inicio'); ?>
        <?php else: ?>
            <?php echo $form->labelEx($model, 'data_inicio'); ?>
            <?php echo Yii::app()->dateFormatter->format('dd/MM/yyyy', $model->data_inicio); ?>
        <?php endif; ?>
    </div>

    <div class="row">
        <?php if ($model->isNewRecord || !$model->data_fim): ?>
            <?php echo $form->labelEx($model, 'data_fim'); ?>
            <?php echo $form->dateField($model, 'data_fim', array('value' => (($model->isNewRecord || !$model->data_fim) ? date('Y-m-d') : $model->data_fim))); ?>
            <?php echo $form->error($model, 'data_fim'); ?>
        <?php else: ?>
            <?php echo $form->labelEx($model, 'data_fim'); ?>
            <?php echo Yii::app()->dateFormatter->format('dd/MM/yyyy', $model->data_fim); ?>
        <?php endif; ?>
    </div>

    <div class="row buttons">
        <?php if ($model->isNewRecord || !$model->data_fim): ?>
            <?php echo CHtml::submitButton($model->isNewRecord ? 'Iniciar' : 'Salvar'); ?>
        <?php else: ?>
            <?php echo CHtml::button('Desculpe, mas você não pode alterar um estágio já finalizado.', array('disabled' => true)); ?>
        <?php endif; ?>
    </div>

    <?php $this->endWidget(); ?>

    <script>
        function autocomplete_render_item(ul, item) {
            return $('<li>')
                .append(item.label + '<br><small>' + item.email + '</small>')
                .appendTo(ul);
        }
        
        $(function(){
            $('#usuario-nome').autocomplete({
                minLength: 1,
                source: '<?php echo Yii::app()->createUrl('administrar/estagio/autocomplete', array('campo' => 'aluno')); ?>',
                select: function(event, ui) {
                    if (ui.item) {
                        $('#usuario-id').val(ui.item.id);
                        $('#usuario-nome').val(ui.item.label);
                    } else {
                        $('#usuario-id').val('');
                    }
                    return false;
                },
                focus: function(event, ui) {
                  $('#usuario-nome').val(ui.item.label);
                  return false;
                }
            })
            .autocomplete( "instance" )._renderItem = autocomplete_render_item;
    
            $('#empresa-nome').autocomplete({
                minLength: 1,
                source: '<?php echo Yii::app()->createUrl('administrar/estagio/autocomplete', array('campo' => 'empresa')); ?>',
                select: function(event, ui) {
                    if (ui.item) {
                        $('#empresa-id').val(ui.item.id);
                        $('#empresa-nome').val(ui.item.label);
                    } else {
                        $('#empresa-id').val('');
                    }
                    return false;
                },
                focus: function(event, ui) {
                  $('#empresa-nome').val(ui.item.label);
                  return false;
                }
            })
            .autocomplete( "instance" )._renderItem = autocomplete_render_item;
        }); 
    </script>
</div><!-- form -->